package by.amakarevich.services.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.springframework.stereotype.Service;

import by.amakarevich.dataaccess.BookingInfoDao;
import by.amakarevich.datamodel.Booking;
import by.amakarevich.datamodel.BookingInfo;
import by.amakarevich.datamodel.Product;
import by.amakarevich.datamodel.ShoppingCartInfo;
import by.amakarevich.datamodel.UserAccount;
import by.amakarevich.services.BookingInfoService;
import by.amakarevich.services.ProductService;
import by.amakarevich.services.ShoppingCartInfoService;

@Service
public class BookingInfoServiceImpl implements BookingInfoService {
	
	@Inject
	private BookingInfoDao bookingInfoDao;
	@Inject
	private ShoppingCartInfoService shoppingCartInfoService;
	@Inject
	private ProductService productService;
	
	@Override
	public void addNewBookingInfo(BookingInfo bookingInfo,  UserAccount userAccount, Booking booking, Product product) {
		bookingInfo.setProduct(product);
		bookingInfo.setBooking(booking);
		bookingInfo.setUserAccount(userAccount);
		bookingInfoDao.insert(bookingInfo);
	}

	@Override
	public BookingInfo get(Long id) {
		return bookingInfoDao.getById(id);
	}

	@Override
	public void updateBookingInfo(BookingInfo bookingInfo) {
		bookingInfoDao.update(bookingInfo);
	}

	@Override
	public void deleteBookingInfo(Long id) {
		bookingInfoDao.delete(id);
	}
	  
	@Override
	    public void deleteAll() {
		bookingInfoDao.deleteAll();
	}
	
	@Override
	public List<BookingInfo> getAllBookingInfo(){
		return bookingInfoDao.getAllBookingInfo();
	}

    public List<BookingInfo> getAllBookingInfoByBooking(SingularAttribute<BookingInfo, ?> attr, boolean ascending, int startRecord, int pageSize){
    	return bookingInfoDao.getAllBookingInfoByBooking(attr, ascending, startRecord, pageSize);
    }
    @Override
    public List<BookingInfo> getAllByBooking(Booking booking){
    	return bookingInfoDao.getAllByBooking(booking);
    };
    
    @Override
    public BookingInfo getByBookingInfoId(Long bookingInfoId){
    	return bookingInfoDao.getByBookingInfoId(bookingInfoId);
    }
    
    @Override
    public void fromShoppingCartToBooking(ShoppingCartInfo shoppingCartInfo, UserAccount userAccount, Booking booking){
    	BookingInfo bookingInfo= new BookingInfo();
    	bookingInfo.setQuantity(shoppingCartInfo.getQuantity());
    	bookingInfo.setProduct(shoppingCartInfo.getProduct());
    	bookingInfo.setPrice(shoppingCartInfo.getPrice());
    	bookingInfo.setUserAccount(userAccount);
    	bookingInfo.setBooking(booking);
    	bookingInfoDao.insert(bookingInfo);
    	Product product=shoppingCartInfo.getProduct();
    	final Long temp;
    		if(product.getAvailable()>shoppingCartInfo.getQuantity()){
				temp=product.getAvailable()-shoppingCartInfo.getQuantity();
			}else{
				temp=0l;
			}
    	product.setAvailable(temp);
    	productService.saveOrUpdate(product);
    	shoppingCartInfoService.deleteShoppingCartInfo(shoppingCartInfo.getId());
    };
}
